package Long.Lu.Service.Impl;


import Long.Lu.Dao.EmployeeDao;
import Long.Lu.Service.EmployeeService;
import Long.Lu.pojo.Employee;
import Long.Lu.pojo.EmployeeParam;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<Employee> selectAll() {
        return employeeDao.findAll();
    }

    @Override
    public boolean updateStatus(Integer employId) {
        try {
            Employee employee = employeeDao.findById(employId).get();
            if (employee != null) {
                Integer status = employee.getStatus();
                if (status == 0) {
                    employee.setStatus(2);
                } else if (status == 1) {
                    employee.setStatus(2);
                } else {
                    employee.setStatus(1);
                }
                this.employeeDao.save(employee);
            }
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }
    //增加员工
    @Override
    public boolean add(Employee employee) {
        employeeDao.save(employee);
        return false;
    }
    //删除员工
    @Override
    public boolean deleteOne(Integer employId) {
        employeeDao.deleteById(employId);
        return true;
    }
    //批量删除
    @Override
    public boolean delAll(String employIds) {
        try{
            if(employIds!=null && !employIds.equals("")) {
                String[] arrs = employIds.split(",");
                employeeDao.deleteAll(Arrays.asList(arrs));
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Page<Employee> select(EmployeeParam employeeParam) {
        Pageable pageable = PageRequest.of(employeeParam.getPage()-1,employeeParam.getRow());
        if(employeeParam.geteName() != null && employeeParam.geteName().equals("")){
            return employeeDao.findByeName(employeeParam.geteName(),pageable);
        }else {
            return employeeDao.findAll(pageable);
        }
    }

    @Override
    public void excelWrite(HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            //创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建HSSFSheet对象
            HSSFSheet sheet = wb.createSheet("员工表");
            //创建HSSFRow对象，先写入列名
            HSSFRow colName = sheet.createRow(0);
            //写入第一行列名
            colName.createCell(0).setCellValue("员工编号");
            colName.createCell(1).setCellValue("员工姓名");
            colName.createCell(2).setCellValue("员工性别");
            colName.createCell(3).setCellValue("员工年龄");
            colName.createCell(4).setCellValue("出生日期");
            colName.createCell(5).setCellValue("部门");
            colName.createCell(6).setCellValue("学历");
            colName.createCell(7).setCellValue("邮箱");
            colName.createCell(8).setCellValue("入职时间");
            colName.createCell(9).setCellValue("电话号码");
            colName.createCell(10).setCellValue("职位");
            colName.createCell(11).setCellValue("备注");
            //查询员工所有信息
            Pageable pageable = PageRequest.of(0,12);
            List<Employee> list = employeeDao.findAll(pageable).getContent();

            for(int i = 1;i <=list.size();i++){
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(list.get(i-1).getEmployId());
                row.createCell(1).setCellValue(list.get(i-1).geteName());
                row.createCell(2).setCellValue(list.get(i-1).getSex());
                row.createCell(3).setCellValue(list.get(i-1).getAge());
                row.createCell(4).setCellValue(list.get(i-1).getBirthDay());
                row.createCell(5).setCellValue(list.get(i-1).getDepartment());
                row.createCell(6).setCellValue(list.get(i-1).getEducation());
                row.createCell(7).setCellValue(list.get(i-1).getEmail());
                row.createCell(8).setCellValue(list.get(i-1).getEntryTime());
                row.createCell(9).setCellValue(list.get(i-1).getPhone());
                row.createCell(10).setCellValue(list.get(i-1).getPost());
                row.createCell(11).setCellValue(list.get(i-1).getStatus());
            }
            //输出Excel文件到页面
            outputStream=response.getOutputStream();
            String fileName="员工表";
            //解决中文文件名下载 变成下划线的问题
            fileName=new String(fileName.getBytes("utf-8"),"ISO8859-1")+"";
            response.reset();
            //设置响应表头
            response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
            //文件格式
            response.setContentType("application/msexcel");

            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
